package com.baidu.serviceimpl;

import com.baidu.mapper.CustomerMapper;
import com.baidu.pojo.CustomerBean;
import com.baidu.service.CustomerService;
import com.baidu.utils.EcityMD5Code;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.UUID;

/**
 * 创作时间：2019/10/28 15:04
 * 作者：李增强
 */
@Service
public class CustomerServiceImpl implements CustomerService {
    @Resource
    CustomerMapper customerMapper;

    @Override
    public CustomerBean saveCusReg(CustomerBean customerBean, String ycode, String scode) {
        if(scode.equals(ycode)){
            EcityMD5Code md = new EcityMD5Code();
            String newpwd = md.getMD5ofStr(customerBean.getCname() + customerBean.getPwd());
            String accode = UUID.randomUUID().toString().replace("-", "").substring(0, 4);
            customerBean.setPwd(newpwd);
            customerBean.setAccode(accode);
            customerMapper.saveCusReg(customerBean);
            return customerBean;
        }
        return null;
    }

    @Override
    public String updateCustomerStatus(Integer id, String accode) {
        if(id!=null){
            CustomerBean cb = customerMapper.getCustomerById(id);
            if(cb.getAccode().equals(accode)){
                if(cb.getStatus()==-1){
                    customerMapper.updateCustomerStatus(cb.getId());
                    return "恭喜你激活成功了";
                }else{
                    return "您已经激活了，不需要重复操作";
                }
            }
        }
        return "激活失败，激活码有误";
    }

    @Override
    public String getCustomerTel(String cname) {
        return customerMapper.getCustomerTel(cname);
    }

    @Override
    public void updatePwd(String cname, String pwd) {
        EcityMD5Code md = new EcityMD5Code();
        pwd = md.getMD5ofStr(cname+pwd);
        customerMapper.updatePwd(cname,pwd);
    }
}
